package graph.day01;

import java.util.Scanner;


public class Kama_99 {

    static int[][] graph;

    static boolean[][] visited;

    static int[][] direction = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

    private static void dfs(int i, int j) {
        visited[i][j] = true;
        for (int[] dir : direction) {
            int x = i + dir[0];
            int y = j + dir[1];
            if (x < 0 || x >= graph.length || y < 0 || y >= graph[0].length) {
                continue;
            }
            if (graph[x][y] == 1 && !visited[x][y]) dfs(x, y);
        }
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        graph = new int[n][m];
        visited = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int x = sc.nextInt();
                graph[i][j] = x;
            }
        }
        int islandCount = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (graph[i][j] == 1 && !visited[i][j]) {
                    islandCount++;
                    dfs(i, j);
                }
            }
        }
        System.out.println(islandCount);
    }
}
